//
//  LocalDetailViewController.m
//  MiaoTu
//
//  Created by dllo on 16/9/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LocalDetailViewController.h"
#import "UIView+Frame.h"
#import "LocalItem.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "ZWYNetTool.h"
#import "LocalDetailModel.h"
#import "LocalDetailItem.h"
#import "ComRes.h"
#import "MJRefresh.h"
#import "LocalDetailTableCell.h"
#import "HotelViewController.h"

#define kLocalDetailTableCell @"LocalDetailTableCellIdentifier"

@interface LocalDetailViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) LocalDetailItem *localDetail;
@property (nonatomic, strong) NSMutableArray<ComRes *> *comResArray;
@end

@implementation LocalDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setUpNavigationBar];
    [self createTableView];
}

#pragma mark - Data
- (void)createData {
    NSString *urlString = [NSString stringWithFormat:@"http://api.miaotu.net/v2/res/recommend/list?area_id=%ld&area_name=%@&page=1&num=10", (long)_localItem.Id, _localItem.Name];
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};

    [ZWYNetTool GET:urlString andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        LocalDetailModel *localDetailModel = [LocalDetailModel modelWithDic:result];
        self.localDetail = localDetailModel.Items;
        self.comResArray = _localDetail.ComRes.mutableCopy;
        [_tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        _currentPage = 1;
        NSLog(@"sss");
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (void)footRefreshData {
    _currentPage += 1;
    NSString *urlString = [NSString stringWithFormat:@"http://api.miaotu.net/v2/res/recommend/list?area_id=%ld&area_name=%@&page=%zd&num=10", (long)_localItem.Id, _localItem.Name, _currentPage];
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};
    [ZWYNetTool GET:urlString andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        LocalDetailModel *model = [LocalDetailModel modelWithDic:result];
        NSArray<ComRes *> *comResArray = model.Items.ComRes;
        for (ComRes *comRes in comResArray) {
            [_comResArray addObject:comRes];
        }
        [_tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailureBlock:^(NSError *error) {
        
    }];
}

#pragma mark - View
- (void)setUpNavigationBar {
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 36, 36);
    backButton.layer.cornerRadius = 18;
    backButton.layer.masksToBounds = YES;
    backButton.layer.borderColor = [UIColor whiteColor].CGColor;
    backButton.layer.borderWidth = 1;
    backButton.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.120];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.dk_width, self.view.dk_height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [self createTableHeaderView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createData)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRefreshData)];
    _tableView.rowHeight = 110;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"LocalDetailTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kLocalDetailTableCell];
}

- (void)createTableHeaderView {
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.dk_width, 320)];
    [backImage sd_setImageWithURL:[NSURL URLWithString:_localItem.Image] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    
    UILabel *countLabel = [UILabel new];
    [backImage addSubview:countLabel];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(backImage);
        make.height.mas_equalTo(@40);
        make.width.mas_equalTo(@180);
    }];
    countLabel.text = [NSString stringWithFormat:@"%ld人在此结伴", (long)self.localItem.YueyouCount];
    countLabel.textColor = [UIColor whiteColor];
    countLabel.backgroundColor = [UIColor colorWithRed:0.910 green:0.294 blue:0.043 alpha:1.000];
    countLabel.layer.cornerRadius = 20;
    countLabel.layer.masksToBounds = YES;
    countLabel.textAlignment = NSTextAlignmentCenter;
    
    UILabel *positionLabel = [UILabel new];
    [backImage addSubview:positionLabel];
    [positionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(countLabel.mas_top).offset(-20);
        make.centerX.mas_equalTo(countLabel.mas_centerX);
    }];
    positionLabel.font = [UIFont systemFontOfSize:28];
    positionLabel.textColor = [UIColor whiteColor];
    positionLabel.text = _localItem.Name;
    
    self.tableView.tableHeaderView = backImage;
}

#pragma mark - TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _comResArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LocalDetailTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kLocalDetailTableCell forIndexPath:indexPath];
    cell.comRes = _comResArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HotelViewController *hotelVC = [[HotelViewController alloc] init];
    hotelVC.comRes = _comResArray[indexPath.row];
    [self.navigationController pushViewController:hotelVC animated:YES];
}

#pragma mark - Action
- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
